﻿/*
 * 作者：新生命开发团队（http://www.newlifex.com/）
 * 
 * 版权：版权所有 (C) 新生命开发团队 2002-2014
 * 
 * 修改：海洋饼干（cuteant@outlook.com）
*/

using System;
using System.Collections.Generic;
using System.Text;

namespace CuteAnt.Log
{
	/// <summary>复合日志提供者，多种方式输出</summary>
	public class CompositeLog : Logger
	{
		#region -- 属性 --

		private List<ILog> _Logs = new List<ILog>();

		/// <summary>日志提供者集合</summary>
		public List<ILog> Logs { get { return _Logs; } set { _Logs = value; } }

		#endregion

		#region -- 构造 --

		/// <summary>实例化</summary>
		public CompositeLog()
		{
		}

		/// <summary>实例化</summary>
		/// <param name="log"></param>
		public CompositeLog(ILog log)
		{
			Logs.Add(log); Level = log.Level;
		}

		/// <summary>实例化</summary>
		/// <param name="log1"></param>
		/// <param name="log2"></param>
		public CompositeLog(ILog log1, ILog log2)
		{
			Add(log1).Add(log2);
			Level = log1.Level;
			if (Level > log2.Level) { Level = log2.Level; }
		}

		#endregion

		/// <summary>添加一个日志提供者</summary>
		/// <param name="log"></param>
		/// <returns></returns>
		public CompositeLog Add(ILog log)
		{
			Logs.Add(log); return this;
		}

		#region -- 写日志 --

		/// <summary>写日志</summary>
		/// <param name="level">日志级别</param>
		/// <param name="msg">信息</param>
		protected override void OnWrite(LogLevel level, String msg)
		{
			if (Logs != null)
			{
				foreach (var item in Logs)
				{
					item.Write(level, msg);
				}
			}
		}

		/// <summary>写带格式信息的日志</summary>
		/// <param name="level">日志级别</param>
		/// <param name="formatMsg">格式化字符串</param>
		/// <param name="args">格式化参数</param>
		protected override void OnWrite(LogLevel level, String formatMsg, params Object[] args)
		{
			if (Logs != null)
			{
				foreach (var item in Logs)
				{
					item.Write(level, formatMsg, args);
				}
			}
		}

		/// <summary>写异常日志</summary>
		/// <param name="level">日志级别</param>
		/// <param name="ex">异常</param>
		protected override void OnWrite(LogLevel level, Exception ex)
		{
			if (Logs != null)
			{
				foreach (var item in Logs)
				{
					item.Write(level, ex);
				}
			}
		}

		/// <summary>写异常日志</summary>
		/// <param name="level">日志级别</param>
		/// <param name="ex">异常</param>
		/// <param name="msg">信息</param>
		protected override void OnWrite(LogLevel level, Exception ex, String msg)
		{
			if (Logs != null)
			{
				foreach (var item in Logs)
				{
					item.Write(level, ex, msg);
				}
			}
		}

		/// <summary>写带格式信息的异常日志</summary>
		/// <param name="level">日志级别</param>
		/// <param name="ex">异常</param>
		/// <param name="formatMsg">格式化字符串</param>
		/// <param name="args">格式化参数</param>
		protected override void OnWrite(LogLevel level, Exception ex, String formatMsg, params Object[] args)
		{
			if (Logs != null)
			{
				foreach (var item in Logs)
				{
					item.Write(level, ex, formatMsg, args);
				}
			}
		}

		/// <summary>从复合日志提供者中提取指定类型的日志提供者</summary>
		/// <typeparam name="TLog"></typeparam>
		/// <returns></returns>
		public TLog Get<TLog>() where TLog : class
		{
			foreach (var item in Logs)
			{
				if (item != null && item is TLog) { return item as TLog; }
			}

			return null;
		}

		#endregion

		//public ILog Get(Type type)
		//{
		//    foreach (var item in Logs)
		//    {
		//        if (item != null && type.IsAssignableFrom(item.GetType())) return item;
		//    }

		//    return null;
		//}

		#region -- 辅助 --

		/// <summary>已重载。</summary>
		/// <returns></returns>
		public override string ToString()
		{
			var sb = new StringBuilder();
			sb.Append(this.GetType().Name);

			foreach (var item in Logs)
			{
				sb.Append(" ");
				sb.Append(item + "");
			}

			return sb.ToString();
		}

		#endregion
	}
}